Confused about GNU `sort(1)` of a numerical sub field

Posted by Chen Levy on Stack Overflow See other posts from Stack Overflow or by Chen Levy
Published on 2010-05-30T09:03:36Z Indexed on 2010/05/30 9:12 UTC
Read the original article Hit count: 264

Filed under:
|
|
|

I wish to sort a space separated table, with the numerical value that found on the 2nd field. I can assume that the 2nd field is always fooN but the length of N is unknown:

antiq. foo11 girls
colleaguing foo2 Leinsdorf
Cousy foo0 Montgomeryville
bowlegged foo1 pollack
Chevrier foo10 ill-conceived
candlebomb foo3 seventieths
autochthony foo101 re-enable
beneficiate foo100 osteometric

I read man sort(1) and played with all sort of options. On my system I found the line:

sort -n -k2.5 table

to work.

My question is why?

According to the man page:

-k, --key=POS1[,POS2]
   start a key at POS1, end it at POS 2 (origin 1) 
...
POS is F[.C][OPTS], where F is the field number and C the characterposition in the
field. OPTS is one or more single-letter ordering options, which override global
ordering options for that key. If no key is given, use the entire line as the key. 

So why sort -n -k2.4 table don't work and sort -n -k.5 does?

© Stack Overflow or respective owner

Related posts about command-line

Related posts about sort